﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;



namespace TsTreeTimeSeries.data
{
    class timeseriesData
    {
        DataClasses1DataContext datacontext = new DataClasses1DataContext();
        string connectString = "Data Source=localhost;Initial Catalog=TsDB;Integrated Security=True";

        public DataTable Getdata(string tableName)
        {
            string sqlSelect = "SELECT * FROM [TsDB].[dbo]." + tableName;
            // Create the data adapter to retrieve data from the database
            SqlDataAdapter da = new SqlDataAdapter(sqlSelect, connectString);
            // Create table mappings
            da.TableMappings.Add("Table", tableName);
            
            // Create and fill the DataSet
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[tableName];
        }
        public List<string> GetAllTables()
        {
            List<string> tables = new List<string>();
            SqlConnection _connection = new SqlConnection(connectString);
            if (_connection.State == System.Data.ConnectionState.Closed)
                _connection.Open();
            DataTable dt = _connection.GetSchema("Tables");
            foreach (DataRow row in dt.Rows)
            {
                string tablename = (string)row[2];
                tables.Add(tablename);
            }
            return tables;
        }

        public void RunStoredProc(int TypeTree, DateTime RunTime, int TotalNode,int InternalNode,int LeafNode,int EntryNum,int Height,float AVGEntryPerNode,float AVGWide,float SearchDuration)
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;

            Console.WriteLine("\nTop 10 Most Expensive Products:\n");

            try
            {
                conn = new SqlConnection(connectString);
                conn.Open();
                SqlCommand cmd = new SqlCommand("dbo.SummaryResult", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@TypeTree", SqlDbType.DateTime).Value = TypeTree;
                cmd.Parameters.Add("@RunTime", SqlDbType.VarChar).Value = RunTime;
                cmd.Parameters.Add("@TotalNode", SqlDbType.DateTime).Value = TotalNode;
                cmd.Parameters.Add("@InternalNode", SqlDbType.VarChar).Value = InternalNode;
                cmd.Parameters.Add("@LeafNode", SqlDbType.DateTime).Value = LeafNode;
                cmd.Parameters.Add("@EntryNum", SqlDbType.VarChar).Value = EntryNum;
                cmd.Parameters.Add("@Height", SqlDbType.DateTime).Value = Height;
                cmd.Parameters.Add("@AVGEntryPerNode", SqlDbType.VarChar).Value = AVGEntryPerNode;
                cmd.Parameters.Add("@AVGWide", SqlDbType.DateTime).Value = AVGWide;
                cmd.Parameters.Add("@SearchDuration", SqlDbType.VarChar).Value = SearchDuration;

                rdr = cmd.ExecuteReader();
                /*while (rdr.Read())
                {
                    Console.WriteLine(
                        "Product: {0,-25} Price: ${1,6:####.00}",
                        rdr["TenMostExpensiveProducts"],
                        rdr["UnitPrice"]);
                }*/
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
        }
    }
}
